/**
 * <p>Title: CNPEC 加密解密 </p>
 * <p>Description: IMS</p>
 * <p>Copyright: Copyright (c) 2009</p>
 * <p>Company: CNPEC</p>
 */
package com.mes.cloud.domain.Encryptor;

import com.mes.cloud.domain.DefaultClient.ToBase64;
import org.apache.commons.codec.binary.Base64;
import sun.misc.BASE64Encoder;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.Provider;
import java.security.Security;

/**
 * 加密解密类
 *
 * @author yang yi
 * @version 1.0 $Id: AESCryption.java,v 1.0 2009/07/20$
 */
public class AESCryption {
    /**
     * 对数据用AES算法加密
     *
     * @param content 要加密的字节数组
     * @param keyStr  密钥
     * @param ivStr   加密向量
     * @return 加密后的字节数组
     */
    public static String encrypt(String content, String keyStr, String ivStr) {
        try {
            // 加密的字符串转换字节型

            byte[] plainText = content.getBytes("UTF-8");
            Base64 objBase64 = new Base64();
            // key和ivStr转换Base64字节
            byte[] key = objBase64.decode(keyStr.getBytes("UTF-8"));
            byte[] ivByte = objBase64.decode(ivStr.getBytes("UTF-8"));
            // 实例化加密类，加密方法为：AES
            SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding", getProvider());
            // 实例化加密向量

            IvParameterSpec iv = new IvParameterSpec(ivByte);
            // 初始化加密类
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
            // 加密
            byte[] decryptText = cipher.doFinal(plainText);
            // 加密码后的字节数组转化为Base64位类型

            return new BASE64Encoder().encode(decryptText);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 对AES算法加密的数据解密
     *
     * @param cipherText 要解密的字节数组
     * @param keyStr     密钥
     * @return 解密后的字节数组
     */
    public static String decrypt(String cipherText, String keyStr, String ivStr) {
        try {
            // cipherText、key和ivStr转换Base64字节
            byte[] cipherContent = ToBase64.decode(cipherText);
            Base64 objBase64 = new Base64();
            byte[] key = objBase64.decode(keyStr.getBytes("UTF-8"));
            byte[] ivByte = objBase64.decode(ivStr.getBytes("UTF-8"));
            // 实例化加密类，加密方法为：AES
            SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding", getProvider());
            // 实例化加密向量

            IvParameterSpec iv = new IvParameterSpec(ivByte);
            // 初始化加密类
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
            // 解密
            byte[] plainText = cipher.doFinal(cipherContent);
            // 解密后的字节数组转化为字符串
            return new String(plainText);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    private static Provider getProvider() {

        Provider[] providers = Security.getProviders();
        Provider p = null;
        for (int i = 0; i < providers.length; i++) {
            if (providers[i].getName().equals("SunJCE")) {
                p = providers[i];
            }
        }
        return p;
    }

    public static void main(String[] a) {
        // String k =
        // "Ey98kQx/XXR2sRlGl9qCIPy1W1xXBdPLzBA3SLHsen/1OhNnY0KFYdPky2OfiPEYRYjTEiY7gjOVM8z5NivgU9oH7Q+0Y8h6LbYVRng6OTg=";
        // k = java.net.URLEncoder.encode(k);
        // String key =
        // "CGWKB9nlx7a0nPJkOOu20lRSHFct2dyJlHRKZV0JnhzUeidWjqeViiipRbFbFbSKQvrztdXK2RyxCsFn3psNtwsWNezdnOV%2bGoFhqMgJvtI%3d";
        // //key =
        // "Ey98kQx%2fXXR2sRlGl9qCIPHiiwv0ZMA3A7%2ftX59fuy667zFenvzSFBtV02v0LEowUznAhaJii0qy%2fiAxXOrjSKnhHqyW%2fW%2bfnCs6UOf%2b%2bMc%3d";
        // key = java.net.URLDecoder.decode(key);
    }
}
